import BaseIndex from './baseIndex';
import { Common } from "UtilsCommon";
import { EnvConfig } from 'Configs';

const _CurrentDataGridViewData = {};

export default class DataGridView extends BaseIndex {
    constructor() {
        super();
        this.initExpand("dataGridView");
    }

    searchQuery(props, action) {
        if (!action.parameters) this.initSearchQueryAction(props, action);
        else action.isSearch = props.property.type !== "DataGridView";

        const isClearQuery = props.isClearQuery || action.isClearQuery;
        const { dataGridView } = action.parameters;

        this.searchData(props, action.parameters, props.pageIndex || 1, props.pageSize || dataGridView.pageSize || 10, isClearQuery, action);
    }

    initSearchQueryAction(props, action) {
        const { property, pageAxis } = props;

        //判断props.property 是 查询按钮或搜索框 还是DataGridView
        const dataGridView = property.type === "DataGridView" ? property : pageAxis.getProperty(action.dataGridView);
        const searchButton = property.type === "DataGridView" ? pageAxis.getProperty(action.searchButton) : property;
        const searchView = pageAxis.getProperty(action.searchView);
        const alertMessage = pageAxis.getProperty(action.alertMessage);
        const expandSearchQueryLoad = pageAxis.getFunction(action.expandSearchQueryLoad);
        const expandSetConditionList = pageAxis.getFunction(action.expandSetConditionList);

        if (dataGridView.isCacheData !== false && Common.isEmptyObject(dataGridView.currentData)) {
            _CurrentDataGridViewData[dataGridView.id] = _CurrentDataGridViewData[dataGridView.id] || {};
            dataGridView.currentData = _CurrentDataGridViewData[dataGridView.id];
            dataGridView.currentData.isInit = true;
        }
        else if (Common.isEmptyObject(dataGridView.currentData)) dataGridView.currentData = {};

        action.parameters = { dataGridView, searchButton, searchView, alertMessage, expandSearchQueryLoad, expandSetConditionList };
    }

    searchData(props, parameters, pageIndex, pageSize, isClearQuery, action) {
        const { dataGridView, searchButton, expandSetConditionList } = parameters;
        const { actionTypes, invokeDataAction } = dataGridView;
        const { searchQuery } = actionTypes;
        const { pageAxis, isData } = props;
        const { dataAction } = action;

        const { currentData } = dataGridView;
        let entitySearchQuery = dataGridView.entitySearchQuery;

        const isInit = currentData.isInit;
        if (currentData.isInit) {
            currentData.isInit = false;
            if (currentData.pageIndex !== undefined) pageIndex = currentData.pageIndex;
            if (currentData.pageSize !== undefined) pageSize = currentData.pageSize;
            if (!currentData.conditions) currentData.conditions = {};
            this.setConditionList(parameters, currentData.conditions);
        }
        else {
            currentData.pageIndex = pageIndex;
            currentData.pageSize = pageSize;
        }

        let conditionList = this.getConditionList(parameters, isClearQuery, currentData, isInit, props);
        if (conditionList === false) return;

        if (expandSetConditionList) {
            conditionList = expandSetConditionList({ conditionList, dataGridView });
            if (conditionList === false) return;
        }

        const queryInfo = this.getQueryInfo(dataGridView);
        queryInfo.WhereFields = conditionList;

        dataGridView.searchButton = searchButton;
        dataGridView.setDataLoading(true);
        dataGridView.queryInfo = queryInfo;
        dataGridView.pageSize = pageSize;
        searchButton && searchButton.setDisabled(true);

        if (!entitySearchQuery && dataAction) {
            entitySearchQuery = this.getDataActionType(pageAxis, dataAction);
        }

        if (dataGridView.isGroupByQuery) dataGridView.entity.isGroupByInfo = true;

        const data = { entitySearchQuery, entity: dataGridView.entity, dataAction, isData, pageIndex, pageSize, queryInfo, pageData: pageAxis.pageData };

        invokeDataAction(searchQuery, data);
    }

    receivesearchQuery(data, props) {
        const { pageAxis, property, isData } = props;
        const action = property.eventActionName ? pageAxis.getEventAction(property.eventActionName) : property.eventAction;
        if (!action.parameters) this.initSearchQueryAction(props, action);
        const { alertMessage, expandSearchQueryLoad } = action.parameters;

        const searchButton = property.searchButton || action.parameters.searchButton;

        let pageRecord = data.pageRecord || 0;
        if (data.pageInfo) pageRecord = data.pageInfo.pageRecord;

        //设置提示信息
        let msg = "";
        if (data.isSuccess === false) {
            msg = data.message;
            if (!alertMessage) this.alert(msg, pageAxis.showMessage);
        }
        else if ((action.isSearch || !action.isQuery) && !isData) msg = `符合当前查询条件的结果总计${pageRecord}条！`;

        action.isQuery = true;

        if (msg && alertMessage) alertMessage.setValue(msg);

        //设置搜索按钮
        searchButton && searchButton.setDisabled(false);

        if (expandSearchQueryLoad) expandSearchQueryLoad({ data, props });
    }

    setConditionList(parameters, conditions) {
        const { searchView } = parameters;
        if (!searchView) return {};

        const queryString = Common.getQueryString();

        searchView.properties.forEach(p => {
            if (p.isCondition) {
                let value = conditions[p.name];
                if (queryString[p.name]) {
                    value = queryString[p.name];
                    conditions[p.name] = value;
                }
                if (value !== undefined) {
                    if (p.setValue) p.setValue(value);
                    p.value = value;
                }
            }
        });
    }

    getConditionList(parameters, isClearQuery, currentData, isInit, props) {
        const { searchView } = parameters;
        if (!searchView) return [];

        const conditionList = [];
        const conditions = {};

        let isCondition = true;
        searchView.properties.forEach(p => {
            const name = p.propertyName || p.name;
            if (p.isCondition && p.getValue) {
                let value = null;
                if (!isClearQuery && currentData.conditions && isInit && currentData.conditions[p.name] !== undefined) value = currentData.conditions[p.name];
                else value = this.getPropertyValues(p, isClearQuery);
                conditionList.push({
                    Name: name,
                    Label: p.label,
                    OperateLogic: p.operateLogic || "=",
                    DataType: p.dataType || "string",
                    Value: value
                });
                conditions[p.name] = value;

                if (p.isNullable === false && Common.isNullOrEmpty(value)) {
                    isCondition = false;
                    return false;
                }
            }
        });

        if (!isCondition) return false;

        if (Common.isArray(searchView.defaultConditions)) {
            let isDefault = true;
            searchView.defaultConditions.forEach(p => {
                const condition = {
                    Name: p.name,
                    Label: p.label,
                    OperateLogic: p.OperateLogic || "=",
                    DataType: p.dataType || "string",
                    Value: p.defaultValue
                };

                if (p.isCurrentUser) condition.Value = Common.getStorage(EnvConfig.loginUserIdKey);
                if (p.isPageData) {
                    condition.Value = props.pageAxis.pageData[p.name];
                    if (Common.isNullOrEmpty(condition.Value) && !p.isNullable) isDefault = false;
                }

                if (!Common.isNullOrEmpty(condition.Value)) conditionList.push(condition);
            });

            if (!isDefault) return false;
        }

        currentData.conditions = conditions;

        return conditionList;
    }

    getPropertyValues(p, isClearQuery) {
        if (isClearQuery) {
            p.setValue(p.defaultValue);
            return p.defaultValue;
        }
        return p.getValue();
    }

    getQueryInfo(dataGridView) {
        const primaryKey = dataGridView.entity.primaryKey;
        let property = null; let isGroupBy = false; let hasPrimaryKey = false; let name = "";
        const queryInfo = {}; const orderByList = []; const groupByFieldList = []; let groupByList = []; const selectFieldList = [];
        let firstFieldOrderBy = "";
        for (let i = 0; i < dataGridView.properties.length; i++) {
            property = dataGridView.properties[i];
            if (dataGridView.isGroupByQuery && property.isVisible === false) continue;

            name = property.propertyName || property.name;
            if (name === primaryKey) hasPrimaryKey = true;
            if (Common.isNullOrEmpty(property.groupByExpression)) {
                selectFieldList.push(name);
                if (!Common.isNullOrEmpty(property.orderByType)) {
                    orderByList.push(name + " " + property.orderByType);
                }
                groupByList.push(name);

                if (!firstFieldOrderBy) firstFieldOrderBy = name + (property.orderByType || " desc");
            }
            else {
                isGroupBy = true;
                groupByFieldList.push(property.groupByExpression + " as " + name);
                if (!Common.isNullOrEmpty(property.orderByType)) {
                    orderByList.push(property.groupByExpression + " " + property.orderByType);
                }

                if (!firstFieldOrderBy) firstFieldOrderBy = property.groupByExpression + (property.orderByType || " desc");
            }
        }

        if (!isGroupBy) {
            groupByList = [];
            if (!hasPrimaryKey) selectFieldList.splice(0, 0, primaryKey);
        }
        else if (orderByList.length === 0) orderByList.push(firstFieldOrderBy);

        queryInfo.HasLabel = selectFieldList.length === 0;
        queryInfo.FieldSql = selectFieldList.join(",");
        queryInfo.OrderBySql = orderByList.join(",");
        queryInfo.GroupBySql = groupByList.join(",");
        queryInfo.GroupByFieldSql = groupByFieldList.join(",");

        return queryInfo;
    }

    getConditions(parameters) {
        const { searchView } = parameters;
        if (!searchView) return [];

        const condition = {};
        searchView.properties.forEach(p => {
            const name = p.propertyName || p.name;
            if (p.isCondition && p.getValue) condition[name] = p.getValue();
        });

        return condition;
    }

    setOrderBy() {

    }

    selectRowToPage(props, action) {
        if (!action.parameters) this.selectRowToPageAction(props, action);

        const { dataGridView, alertMessage, setPageUrl } = action.parameters;
        const { pageAxis, property } = props;

        let data = null;
        if (property.params) data = property.params;
        else {
            const selectDataList = dataGridView.getSelectDataList();
            if (selectDataList.length === 0) {
                this.alert("请选择记录再操作！", pageAxis.showMessage, alertMessage);
                return;
            }

            if (selectDataList.length > 0 && alertMessage) alertMessage.setValue("");

            data = selectDataList[0];
        }

        if (action.isLocalData) {
            const editData = {};
            editData[dataGridView.entity.name] = data;
            Common.setStorage(EnvConfig.editEntityDataKey, JSON.stringify(editData));
        }

        let url = "";
        if (setPageUrl) {
            url = setPageUrl({ data, props, action });
            if (url === false) return false;
        }
        else {
            url = Common.replaceDataContent({ ...pageAxis.pageData, ...data }, action.pageUrl, true);
            if (action.menuName) url = Common.addUrlParams(url, 'menuName', action.menuName);
        }

        const expandSetPageUrl = pageAxis.getFunction(action.expandSetPageUrl);
        if (expandSetPageUrl) url = expandSetPageUrl(url);

        if (action.isOpenUrl) pageAxis.openPage(url);
        else pageAxis.toPage(url);
    }

    alertByRowData(props, action) {
        if (!action.parameters) this.alertByRowDataAction(props, action);

        const { dataGridView, alertMessage } = action.parameters;
        const { pageAxis } = props;

        const selectDataList = dataGridView.getSelectDataList();
        if (selectDataList.length === 0) {
            this.alert("请选择记录再操作！", pageAxis.showMessage, alertMessage);
            return;
        }

        const data = selectDataList[0];

        const { statusName, statusValue, nullTipMessage } = action;
        if (statusName && statusValue) {
            if (!Common.isEquals(data[statusName], statusValue)) {
                pageAxis.alert(nullTipMessage);
                return;
            }
        }

        const msg = Common.replaceDataContent(data, action.TipMessage);
        pageAxis.alert(msg, action.title);
    }

    selectRowToPageAction(props, action) {
        const { pageAxis } = props;
        const dataGridView = pageAxis.getProperty(action.dataGridView);
        const alertMessage = pageAxis.getProperty(action.alertMessage);
        const setPageUrl = pageAxis.getFunction(action.setPageUrl);

        action.parameters = { dataGridView, alertMessage, setPageUrl };
    }

    alertByRowDataAction(props, action) {
        const { pageAxis } = props;
        const dataGridView = pageAxis.getProperty(action.dataGridView);
        const alertMessage = pageAxis.getProperty(action.alertMessage);

        action.parameters = { dataGridView, alertMessage };
    }

    excelExport(props, action) {
        if (!action.parameters) this.initExcelExportActoin(props, action);

        const { dataGridView } = action.parameters;
        const { pageAxis } = props;

        if (dataGridView.getPageRecord() > 30000) { pageAxis.alert("对不起，您要导出的数据量超过3万条，请先进行相应的数据筛选！"); return; }

        pageAxis.confirm("确定将数据Excel导出吗？", () => this.execExcelExport(props, action));
    }

    execExcelExport(props, action) {
        const { dataGridView, expandSetExcelExportQueryInfo } = action.parameters;
        const { pageAxis, property } = props;

        const { actionTypes, invokeDataAction } = dataGridView;
        const { excelExport } = actionTypes;
        const { dataAction } = action;

        const title = property.title || dataGridView.title || dataGridView.entity.name;

        const properties = dataGridView.getExcelExportProperties();
        const headerList = [];
        let header = {}; let p = null;
        for (let i = 0; i < properties.length; i++) {
            p = properties[i];
            if (p.isData !== false) {
                header = {};
                header.Label = p.label;
                header.Name = p.name;
                header.width = p.columnWidth || 0;
                headerList.push(header);
            }
        }

        let queryInfo = Common.clone(dataGridView.queryInfo);
        queryInfo.HeaderInfos = headerList;

        if (expandSetExcelExportQueryInfo) queryInfo = expandSetExcelExportQueryInfo(queryInfo);
        if (queryInfo === false) return;

        property && property.setDisabled(true);
        dataGridView.excelExportButton = property;

        let entityExcelExport = dataGridView.entityExcelExport;
        if (!entityExcelExport && dataAction) {
            entityExcelExport = this.getDataActionType(pageAxis, dataAction);
        }

        const data = { entityExcelExport, title, entity: dataGridView.entity, dataAction, pageIndex: 1, pageSize: dataGridView.pageSize, queryInfo, pageData: pageAxis.pageData };

        invokeDataAction(excelExport, data);
    }

    initExcelExportActoin(props, action) {
        const { pageAxis } = props;
        const dataGridView = pageAxis.getProperty(action.dataGridView);

        const expandSetExcelExportQueryInfo = pageAxis.getFunction(action.expandSetExcelExportQueryInfo);

        action.parameters = { dataGridView, expandSetExcelExportQueryInfo };
    }

    receiveexcelExport(data, props) {
        const { pageAxis, property } = props;

        //设置导出按钮
        property.excelExportButton.setDisabled(false);

        if (data.isSuccess === false || !data.FileUrl) {
            this.alert(data.message || "导出失败！", pageAxis.showMessage);
            return;
        }

        const title = data.Title || property.excelExportButton.title;

        this.downLoad(title, data.FileUrl);
    }

    downLoad(title, fileUrl) {
        window.open(fileUrl, "_self");
    }

    syncData(props, action) {
        if (!action.parameters) this.initSyncDataAction(props, action);
        const { pageAxis, property } = props;

        const { dataActionType } = property;

        //设置接收数据行数返回数据
        pageAxis.receives[dataActionType] = (d) => this.receiveSyncDataList(d, props, action);

        const onOk = () => {
            property.setDisabled && property.setDisabled(true);

            pageAxis.invokeDataAction(dataActionType, {});
        };

        if (property.confirmTip) pageAxis.confirm(property.confirmTip, onOk);
        else onOk();
    }

    receiveSyncDataList(data, props, action) {
        const { dataGridView } = action.parameters;
        const { pageAxis, property } = props;

        property.setDisabled && property.setDisabled(false);

        if (this.isSuccessNextsProps(data, pageAxis.alert)) {
            this.alert(property.successTip, pageAxis.showMessage);
            //刷新查询
            dataGridView.refresh();
        }
        return false;
    }

    initSyncDataAction(props, action) {
        const { pageAxis } = props;
        const dataGridView = pageAxis.getProperty(action.dataGridView);

        action.parameters = { dataGridView };
    }
}